// 笔记中超详细 https://www.jianshu.com/p/37a8ac155549
class MyPromise {
    constructor(executor) {
        try {
            executor(this.resolve, this.reject);
        } catch (e) {
            this.reject(e);
        }
    }
    status = 'pending'
    value = undefined
    reason = undefined
    successCallback = []
    failCallback = []
    resolve = (value)=>{
        if(this.status !== 'pending') {
            return;
        }
        this.status = 'fulfilled';
        this.value = value;
        while(this.successCallback.length) {
            this.successCallback.shift()();
        }
    }
    reject = (reason)=>{
        if(this.status !== 'pending') {
            return;
        }
        this.status = 'rejected';
        this.reason = reason;
        while(this.failCallback.length) {
            this.failCallback.shift()();
        }
    }

    then (successCallback, failCallback) {
        successCallback = successCallback ? successCallback : (v) => v;
        failCallback = failCallback ? failCallback : (reason) => {
            throw reason;
        };
        let x;
        let promise2 = new MyPromise((resolve, reject)=>{
            if(this.status === 'fulfilled') {
                try {
                    x = successCallback(this.value);
                    this.resolvePromise(x, resolve, reject);
                } catch (e) {
                    this.reject(e);
                }
            } else if (this.status === 'rejected') {
                try {
                    x = failCallback(this.reason);
                    this.resolvePromise(x, resolve, reject);
                } catch (e) {
                    this.reject(e);
                }
            } else {
                this.successCallback.push(()=>{
                    try {
                        x = successCallback(this.value);
                        this.resolvePromise(x, resolve, reject);
                    } catch (e) {
                        this.reject(e);
                    }
                });
                this.failCallback.push(()=>{
                    try {
                        x = failCallback(this.reason);
                        this.resolvePromise(x, resolve, reject);
                    } catch (e) {
                        this.reject(e);
                    }
                });
            }
        });
        return promise2;
    }
    resolvePromise(x, resolve, reject) {
        if(x instanceof MyPromise) {
            x.then((value)=>{
                resolve(value);
            }, (reason)=>{
                reject(reason);
            });
        } else {
            resolve(x);
        }
    }

    static all(arr) {
        return new MyPromise((resolve, reject)=>{
            let result = [];
            let index = 0;
            for(let i = 0; i < arr.length; i++) {
                let current = arr[i];
                if(current instanceof MyPromise) {
                    current.then(value => {
                        addData(i, value);
                    }, reason => reject(reason));
                }else{
                    addData(i, current);
                }
            }

            function addData (key, value) {
                result[key] = value;
                index++;
                if (index === arr.length) {
                    resolve(result);
                }
            }
        });
    }


    static resolve (value) {
        if (value instanceof MyPromise) return value;
        return new MyPromise(resolve => resolve(value));
    }

    finally(callback) {
        return this.then(value => {
            return MyPromise.resolve(callback()).then(() => value);
        }, reason => {
            return MyPromise.resolve(callback()).then(() => { throw reason })
        });
    }

    catch(callback) {
        return this.then(undefined, callback);
    }

}